<!-- 待处理隐患台账  -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编号" prop="taskCode">
        <el-input v-model="queryParams.taskCode" placeholder="请输入任务编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="queryParams.taskName" placeholder="请输入任务名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="设施类型" prop="deviceClassification">
        <el-select v-model="queryParams.deviceClassification" placeholder="请选择设施类型" clearable>
          <el-option v-for="dict in dict.type.task_device_classification" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" placeholder="请选择设备类型" clearable>
          <el-option v-for="dict in dict.type.task_device_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属街道" prop="streetId">
        <el-select v-model="queryParams.streetId" placeholder="请选择街道" clearable>
          <el-option v-for="dict in dict.type.bus_neighborhoodledger" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属小区" prop="neighborhoodId">
        <el-select v-model="queryParams.neighborhoodId" placeholder="请选择小区" clearable>
          <el-option v-for="dict in dict.type.bus_neighborhood" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择班组" clearable>
          <el-option v-for="dict in dict.type.bus_peoson_group" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人员" prop="personId">
        <el-select v-model="queryParams.personId" placeholder="请选择人员" clearable>
          <el-option v-for="dict in dict.type.bus_person_name" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>



      <el-form-item label="状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.bus_task_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:risk:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="riskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" type="index" width="100" align="center" v-if="getColumn('index')">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
</el-table-column> -->
      <!-- <el-table-column label="主键" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column label="任务编号" align="center" prop="taskCode" v-if="getColumn('taskCode')" />
      <el-table-column label="任务名称" align="center" prop="taskName" v-if="getColumn('taskName')" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" v-if="getColumn('deviceCode')" />
      <el-table-column label="设施类型" align="center" prop="deviceClassification" v-if="getColumn('deviceClassification')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_device_classification" :value="scope.row.deviceClassification" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="deviceType" v-if="getColumn('deviceType')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_device_type" :value="scope.row.deviceType" />
        </template>
      </el-table-column>
      <el-table-column label="所属街道" align="center" prop="streetId" v-if="getColumn('streetId')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhoodledger" :value="scope.row.streetId" />
        </template>
      </el-table-column>
      <el-table-column label="所属小区" align="center" prop="neighborhoodId" v-if="getColumn('neighborhoodId')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhood" :value="scope.row.neighborhoodId" />
        </template>
      </el-table-column>

      <el-table-column label="详细地址" align="center" prop="address" v-if="getColumn('address')" />
      <el-table-column label="所属班组" align="center" prop="teamId" v-if="getColumn('teamId')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_peoson_group" :value="scope.row.teamId" />
        </template>
      </el-table-column>
      <el-table-column label="负责人员" align="center" prop="personId" v-if="getColumn('personId')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_person_name" :value="scope.row.personId" />
        </template>
      </el-table-column>


      <el-table-column label="状态" align="center" prop="taskStatus" v-if="getColumn('taskStatus')">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_task_status" :value="scope.row.taskStatus" />
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="taskStatusNumber" v-if="getColumn('taskStatusNumber')" />
      <el-table-column label="隐患照片" align="center" prop="taskImages" v-if="getColumn('taskImages')" />
      <el-table-column label="备注" align="center" prop="taskRemarks" v-if="getColumn('taskRemarks')" />

      <el-table-column label="发现时间" align="center" prop="discoveryTime" width="180" v-if="getColumn ('discoveryTime')">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.discoveryTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="getColumn('operate')">
        <template slot-scope="scope">
          
          <el-button v-if="scope.row.confirmStatus == 0" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:risk:confirm']" >未确认</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:risk:confirm']" v-if="scope.row.confirmStatus == 1">已确认</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:risk:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:risk:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :pageSizes="pageSizes" :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { listRisk, getRisk, delRisk, addRisk, updateRisk } from "@/api/business/risk";

export default {
  name: "Risk",
  dicts: ['task_device_type', 'task_device_classification', 'bus_neighborhood', 'bus_person_name', 'bus_task_status', 'bus_neighborhoodledger', 'bus_peoson_group', 'bus_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待处理隐患台账表格数据
      riskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceType: null,
        streetId: null,
        neighborhoodId: null,
        teamId: null,
        personId: null,
        taskCode: null,
        taskName: null,
        deviceClassification: null,
        taskStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
      },
      // 列信息
      columns: [
        { key: 'index', label: `序号`, visible: true },
        { key: 'id', label: '主键', visible: true },
        { key: 'deviceCode', label: '设备编号', visible: true },
        { key: 'deviceType', label: '设备类型', visible: true },
        { key: 'address', label: '详细地址', visible: true },
        { key: 'streetId', label: '街道ID', visible: true },
        { key: 'neighborhoodId', label: '小区ID', visible: true },
        { key: 'personId', label: '人员ID', visible: true },
        { key: 'taskCode', label: '任务编号', visible: true },
        { key: 'taskName', label: '任务名称', visible: true },
        { key: 'deviceClassification', label: '设施类型', visible: true },
        { key: 'taskStatus', label: '状态', visible: true },
        { key: 'taskStatusNumber', label: '数量', visible: true },
        { key: 'taskImages', label: '维保图片', visible: true },
        { key: 'taskRemarks', label: '备注', visible: true },
        { key: 'discoveryTime', label: '发现时间', visible: true },

        { key: 'operate', label: `操作`, visible: true }

      ],
      // 分页参数
      pageSizes: [10, 50, 100, 500, 1000]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待处理隐患台账列表 */
    getList() {
      this.loading = true;
      listRisk(this.queryParams).then(response => {
        this.riskList = response.rows;
        console.log(this.riskList);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
        taskUniCode: null,
        deviceCode: null,
        deviceType: null,
        deviceTypeLabel: null,
        address: null,
        streetId: null,
        streetName: null,
        neighborhoodId: null,
        neighborhoodName: null,
        teamId: null,
        teamName: null,
        personId: null,
        personName: null,
        taskCode: null,
        taskName: null,
        deviceId: null,
        deviceClassification: null,
        deviceClassificationLabel: null,
        taskStatus: null,
        taskStatusNumber: null,
        taskImages: null,
        taskRemarks: null,
        completeTime: null,
        completePersonId: null,
        confirmStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待处理隐患台账";
    },
    /** 确认按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRisk(id).then(response => {
        this.form = response.data;
        // console.log(this.form);
          if (this.form.id != null && this.form.confirmStatus == 0) {
            // console.log(this.form.confirmStatus);
            this.form.confirmStatus = 1;
            updateRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }else{
            this.$modal.msgError("该隐患已确认完成");
          }
      });    
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRisk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRisk(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
 
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/risk/export', {
        ...this.queryParams
      }, `risk_${new Date().getTime()}.xlsx`)
    },
    /** 列是否显示 */
    getColumn(key) {
      const res = this.columns.filter(item => item.key === key);
      if (res.length >= 1) {
        return res[0].visible
      } else {
        return false
      }
    }
  }
};
</script>
